Date.fromShortDate = function(val) {

    if (val instanceof Date) return val;
    if (!val) return new Date();

    var dateParts = val.split("/");

    if (dateParts.length == 2) {
        return new Date(dateParts[1], (dateParts[0] - 1), 1);
    }

    if (dateParts.length == 3) {
        return new Date(dateParts[2], (dateParts[0] - 1), dateParts[1]);
    }

    return new Date();
};


Date.daysBetween = function (start, end) {
    //Get 1 day in milliseconds
    var ms = 1000 * 60 * 60 * 24;

    // Convert both dates to milliseconds
    var sms = Date.fromShortDate(start);
    var ems = Date.fromShortDate(end);

    // Calculate the difference in milliseconds
    var dms = ems - sms;

    // Convert back to days and return
    return Math.round(dms / ms);
};

/**
 * Parse a string that looks like time and return a date object.
 * @return  Date object on success, false on error.
 */
String.prototype.parseTime = function(date) {
    // trim it and reverse it so that the minutes will always be greedy first:

    var parts = this.split(' ');

    // reverse it:
    var timeParts = parts[0].split(':');

    // Parse out the sections:
    var minutes = parseInt(timeParts[1]);
    var hours = parseInt(timeParts[0]);

    var afternoon = parts[1].toLowerCase() == 'pm' ? true : false;

    if (afternoon) {
        hours += 12;
        if (hours == 24) hours = 12;
    } else {
        if (hours == 12) hours = 0;
    }
    
    // Return a date object with these values set.
    var d = date ? new Date(date) : new Date();

    d.setHours(hours);
    d.setMinutes(minutes);
    return d;
};

